   package com.handong.handserialprogram.module.tasklist;

import com.google.gson.Gson;
import com.handong.handserialprogram.model.MissionModel;
import com.handong.handserialprogram.model.entities.MissionEntity;
import com.handong.handserialprogram.module.base.BasePresenter;
import com.handong.handserialprogram.module.base.BaseView;
import com.handong.handserialprogram.ui.LoginBottomDialog;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.AsyncHttpUtil;
import com.handong.handserialprogram.utils.JLog;
import com.handong.handserialprogram.utils.SPUtil;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.util.List;

/**
 * Created by Administrator on 2016/12/10.
 */

public class TaskListPresenter extends BasePresenter<TaskListActivity> implements TaskListContract.Presenter, LoginBottomDialog.LoginCallback {

    private static final String TAG = TaskListPresenter.class.getSimpleName();
    //private DBManager mDBManager;

    private List<MissionEntity> mMissionList;
    private LoginBottomDialog mLoginBottomDialog;

    public TaskListPresenter(BaseView view) {
        super(view);
    }

    protected void showLoginDialog() {
        if (mLoginBottomDialog == null) {
            mLoginBottomDialog = LoginBottomDialog.create(mView.getSupportFragmentManager(), this);
        }
        mLoginBottomDialog.show();
    }

    @Override
    public void loginUserSuccess(String userName) {
        mLoginBottomDialog.dismiss();
        mLoginBottomDialog = null;
        mView.loginUserSuccess(userName);
    }

    @Override
    public void loadTaskList() {
        if (!AsyncHttpUtil.isNetworkConnected(mView)) {
            mView.showLoadingFail();
            return;
        }
        RequestParams params = new RequestParams("visit", "GetMissByState");
        params.put("state", 1);
        params.put("pPhone", SPUtil.getStringValue(SPUtil.PHONE_NAME));
        params.put(SPUtil.COMPANY_CODE, SPUtil.getStringValue(SPUtil.LOGIN_CODE));
        AsyncHttpUtil.postRequest(params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                JLog.d(TAG, throwable.toString());
                if (mView == null) {
                    return;
                }
                mView.showLoadingFail();
                AppUtil.showShortToast(mView, "网络连接异常,请检查配置");
            }

            @Override
            public void onRetry(int retryNo) {
                JLog.d(TAG, "onRetry:" + retryNo);
                super.onRetry(retryNo);
            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                JLog.d(TAG, s);
                if (mView == null) {
                    return;
                }
                if (s.length() < 15) {
                    mView.showLoadingFail();
                    return;
                }
                Gson gson = new Gson();
                MissionModel missionModel = gson.fromJson(s, MissionModel.class);
                if (missionModel.isSuccess()) {
                    mMissionList = missionModel.getEscortMission();
                    if (mMissionList.isEmpty()) {
                        mView.noExecutingMission();
                        return;
                    }
                    //显示数据
                    mView.showTaskList(mMissionList);
                } else {
                    mView.showLoadingFail();
                }
            }
        });
    }

    /**
     * 从网络获取数据失败，则从数据库读取。
     */
 /*   private void loadMissionFail() {

        //if (mDBManager == null) {
        //mDBManager = DBManager.getDBManager(mView.getApplicationContext());
        //}

        //mMissionList = mDBManager.queryAllMission();

        if (mMissionList == null || mMissionList.size() == 0) {
            mView.showLoadingFail();
        } else {
            mView.showTaskList(mMissionList);
        }
    }*/

    @Override
    public void queryMissionInList(String newText) {

        List<MissionEntity> list = mMissionList;
        int len = list.size();
        MissionEntity entity = null;
        for (int i = 0; i < len; i++) {
            entity = list.get(i);
            if (entity.getTaskName().contains(newText)) {
                mView.moveToSearchResult(i);
                JLog.d("Search", "over");
                break;
            }
        }
    }

    @Override
    public void refreshMissionList(int index, int type) {
        if (mMissionList != null) {
            //mMissionList.get(index).setTaskStatue(type);
        }
    }

    /**
     *  检查未完成的任务
     */
    protected boolean isMissionExecuting() {
        if (mMissionList == null) {
            mView.showLoadDialog("任务列表加载中...");
            loadTaskList();
            //AppUtil.showShortToast(mView, "请先获取任务");
            return true;
        }

        if (mMissionList.isEmpty()) {
            return false;
        }
        for (MissionEntity entity : mMissionList) {
            if (entity.ispInTask()) {
                mView.showExecutingMissDialog(entity);
                return true;
            }
        }
        return false;
    }

    @Override
    protected void exit() {
        super.exit();
    }
}
